/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/11/28
*/
#include "LuaEngine.h"

#ifdef EDUI_USE_LUAPLUS

EDUI_NAMESPACE_BEGIN

namespace LuaEngine{

int LuaGetVersion(LuaState *L)
{
    L->PushString(IEnginePtr->GetVersion());
    return 1;
}

int LuaSetClipboardText(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    if (args[1].IsString()){
        bRet = IEnginePtr->SetClipboardText(args[1].GetString());
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaGetClipboardText(LuaState *L)
{
    const char* str = IEnginePtr->GetClipboardText();
    if (str) L->PushString(str);
    else L->PushNil();
    return 1;
}

int LuaHasClipboardText(LuaState *L)
{
    L->PushBoolean(IEnginePtr->HasClipboardText());
    return 1;
}

int GetKeyFocusWindow(LuaState *L)
{
    IWindow* wnd = IEnginePtr->GetKeyFocusWindow();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, wnd);
    obj.Push(L);
    return 1;
}

int GetMouseFocusWindow(LuaState *L)
{
    IWindow* wnd = IEnginePtr->GetMouseFocusWindow();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, wnd);
    obj.Push(L);
    return 1;
}

int LuaLoadRes(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    if (args[1].IsString()){
        bool b = false;
        if (args[2].IsBoolean()) b = args[2].GetBoolean();
        bRet = IEnginePtr->LoadRes(args[1].GetString(), b);
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaLoadResFromStr(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    if (args[1].IsString()){
        bRet = IEnginePtr->LoadResFromStr(args[1].GetString());
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaLoadWindow(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsString()){
        bool b = false;
        if (args[2].IsBoolean()) b = args[2].GetBoolean();
        IWindow* wnd = IEnginePtr->LoadWindow(args[1].GetString(), b);
        LuaObject obj = LuaHelperNS::GetFromObjs(L, wnd);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaLoadWindowFromStr(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsString()){
        IWindow* wnd = IEnginePtr->LoadWindowFromStr(args[1].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, wnd);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LoadString(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    if (args[1].IsString() && args[2].IsString()){
        const char* group = 0;
        int id = -1;
        if (args[3].IsString()) group = args[3].GetString();
        if (args[4].IsInteger()) id = args[4].GetInteger();
        bRet = IEnginePtr->LoadString(args[1].GetString(), args[2].GetString(), group, id);
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaSetStringGroup(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsString()){
        IEnginePtr->SetStringGroup(args[1].GetString());
    }
    return 0;
}

int LuaGetStringGroup(LuaState *L)
{
    const char* str = IEnginePtr->GetStringGroup();
    if (str) L->PushString(str);
    else L->PushNil();
    return 1;
}

int LuaGetText(LuaState *L)
{
    LuaStack args(L);
    const char* nilText = 0;
    if (args[2].IsString()) nilText = args[2].GetString();
    if (args[1].IsString()){
        nilText = IEnginePtr->GetText(args[1].GetString(), nilText);
    }
    if (nilText) L->PushString(nilText);
    else L->PushNil();
    return 1;
}

int LuaGetTextByID(LuaState *L)
{
    LuaStack args(L);
    const char* nilText = 0;
    if (args[2].IsString()) nilText = args[2].GetString();
    if (args[1].GetInteger()){
        nilText = IEnginePtr->GetText(args[1].GetInteger(), nilText);
    }
    if (nilText) L->PushString(nilText);
    else L->PushNil();
    return 1;
}

int LuaSetXmlDir(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsString()){
        IEnginePtr->SetXmlDir(args[1].GetString());
    }
    return 0;
}

int LuaGetXmlDir(LuaState *L)
{
    const char* str = IEnginePtr->GetXmlDir();
    if (str) L->PushString(str);
    else L->PushNil();
    return 1;
}

int FindWindow(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsString()){
        IWindow* wnd = IEnginePtr->FindWindow(args[1].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, wnd);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int FindWindowByID(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsInteger()){
        IWindow* wnd = IEnginePtr->FindWindow(args[1].GetInteger());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, wnd);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int FindFont(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsString()){
        IFont* font = IEnginePtr->FindFont(args[1].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, font);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int FindFontByID(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsInteger()){
        IFont* font = IEnginePtr->FindFont(args[1].GetInteger());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, font);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int FindCursor(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsString()){
        ICursor* cursor = IEnginePtr->FindCursor(args[1].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, cursor);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int FindCursorByID(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsInteger()){
        ICursor* cursor = IEnginePtr->FindCursor(args[1].GetInteger());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, cursor);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int FindSurface(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsString()){
        ISurface* surface = IEnginePtr->FindSurface(args[1].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, surface);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int FindSurfaceByID(LuaState *L)
{
    LuaStack args(L);
    if (args[1].IsInteger()){
        ISurface* surface = IEnginePtr->FindSurface(args[1].GetInteger());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, surface);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

//----------------------------------------//

void Register(LuaState *L)
{
    LuaObject engine = L->GetGlobal("EDUI").CreateTable("Engine");
    engine.Register("GetVersion", LuaGetVersion);
    engine.Register("SetClipboardText", LuaSetClipboardText);
    engine.Register("GetClipboardText", LuaGetClipboardText);
    engine.Register("HasClipboardText", LuaHasClipboardText);

    engine.Register("GetKeyFocusWindow", GetKeyFocusWindow);
    engine.Register("GetMouseFocusWindow", GetMouseFocusWindow);

    engine.Register("LoadRes", LuaLoadRes);
    engine.Register("LoadResFromStr", LuaLoadResFromStr);

    engine.Register("LoadWindow", LuaLoadWindow);
    engine.Register("LoadWindowFromStr", LuaLoadWindowFromStr);
    engine.Register("LoadString", LoadString);

    engine.Register("FindWindow", FindWindow);
    engine.Register("FindWindowByID", FindWindowByID);
    engine.Register("FindFont", FindFont);
    engine.Register("FindFontByID", FindFontByID);
    engine.Register("FindCursor", FindCursor);
    engine.Register("FindCursorByID", FindCursorByID);
    engine.Register("FindSurface", FindSurface);
    engine.Register("FindSurfaceByID", FindSurfaceByID);

    engine.Register("SetStringGroup", LuaSetStringGroup);
    engine.Register("GetStringGroup", LuaGetStringGroup);
    engine.Register("GetText", LuaGetText);
    engine.Register("GetTextByID", LuaGetTextByID);
    engine.Register("SetXmlDir", LuaSetXmlDir);
    engine.Register("GetXmlDir", LuaGetXmlDir);
}

}
EDUI_NAMESPACE_END

#endif